package com.message.api.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.message.api.base.entity.MsText;
import com.message.api.base.service.MsTextService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-08-10
 */
@Api(tags = "文章管理")
@RestController
@RequestMapping("/msText")
public class MsTextController extends BaseController {

    private static final Logger        LOG = LoggerFactory.getLogger(MsTextController.class);
    @Autowired
    private              MsTextService entityService;

    @SysLog(action = "msText:findByPage", value = "分页查询文章")
    @ApiOperation(value = "分页查询文章", notes = "分页查询文章")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<MsText>> findByPage(final MsText example, final Page page) {
        IPage<MsText> records = entityService.page(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "msText:delete", value = "删除文章")
    @ApiOperation(value = "删除文章")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        entityService.removeById(id);
        return new Result(true, "成功删除文章", null);
    }

    @SysLog(action = "msText:one", value = "查询单个文章")
    @ApiOperation(value = "查询单个文章")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        MsText entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "msText:add", value = "添加文章")
    @ApiOperation(value = "添加文章")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final MsText entity) {
        entity.setTextState(0);
        entityService.save(entity);
        return new Result(true, "成功保存文章", null);
    }

    @SysLog(action = "msText:update", value = "修改文章")
    @ApiOperation(value = "修改文章")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final MsText entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改文章", null);
    }

    @SysLog(action = "msText:all", value = "全部文章")
    @ApiOperation(value = "全部文章")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<MsText>> all(MsText example) {
        List<MsText> entitys = entityService.list(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

    @SysLog(action = "msText:publishState", value = "发布文章")
    @ApiOperation(value = "发布文章")
    @RequestMapping(value = "publishState", method = {RequestMethod.PUT})
    public Result textPublish(@Valid @RequestBody final MsText msText) {
        if (null == msText.getId()) {
            throw new RuntimeException("id不能为空");
        }
        //如果发布一篇文章，先将所有文章改为不发布，保证只有一篇文章发布
//        if(msText.getTextState()==1){
            entityService.lambdaUpdate().set(MsText::getTextState,0).update();
//        }
        msText.setTextState(1);
        entityService.updateById(msText);
        return new Result(true, "更改成功", null);
    }
    @SysLog(action = "msText:publish", value = "查询发布文章")
    @ApiOperation(value = "查询发布文章")
    @RequestMapping(value = "publish", method = RequestMethod.GET)
    public Result publish() {
        MsText entity = entityService.lambdaQuery().eq(MsText::getTextState,1).one();
        return new Result(true, entity);
    }
}

